<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    用户名：<input type="text" id="username" /> 密码：<input
      type="text"
      id="password"
    />
    <button id="dl">登录</button>
    <button id="zc">注册</button>
    <script>
      let usernames = document.querySelector("#username");
      let passwords = document.querySelector("#password");
      let dl = document.querySelector("#dl");
      let zc = document.querySelector("#zc");
      let persons = [
        { username: "xiaoming1", password: "1111" },
        { username: "xiaoming2", password: "222" },
        { username: "xiaoming3", password: "33333" },
      ];
      zc.onclick = () => {
        let user = { username: usernames.value, password: passwords.value };
        if (usernames.value === "" || passwords.value === "") {
          alert("内容不能为空");
          return;
        }
        let n = persons.some((item) => item.username === user.username);
        console.log(n);
        if (n) {
          alert("该用户已注册");
        } else {
          persons.push(user);

          usernames.value = "";
          passwords.value = "";
          alert("注册成功");
        }
      };

      dl.onclick = () => {
        if (usernames.value === "" || passwords.value === "") {
          alert("内容不能为空");
          return;
        }
        let n = persons.some(
          (item) =>
            item.username === usernames.value &&
            item.password === passwords.value
        );
        // let n = persons.some(function (item) {
        //   return (
        //     item.username === usernames.value &&
        //     item.password === passwords.value
        //   );
        if (n) {
          usernames.value = "";
          passwords.value = "";
          alert("登录成功");
        } else {
          usernames.value = "";
          passwords.value = "";
          alert("用户名密码错误");
        }
      };
    </script>
  </body>
</html>
